package com.xkdx.guangguang.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.shareclass.Shop;

public class ShopAdapter extends BaseAdapter {

	String url;
	Context context;
	List<Shop> list;
	ImageLoader imageLoader;
	DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.focuse_brand_discount)
			.showImageForEmptyUri(R.drawable.focuse_brand_discount).showImageOnFail(R.drawable.focuse_brand_discount)
			.resetViewBeforeLoading().cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
			.bitmapConfig(Bitmap.Config.RGB_565).build();

	public ShopAdapter(Context context, List<Shop> list, ImageLoader imageLoader) {
		super();
		this.list = list;
		this.context = context;
		this.imageLoader = imageLoader;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHodler holder;

		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.shop_item, null);
			holder = new ViewHodler();
			holder.image = (ImageView) convertView.findViewById(R.id.series_item_image);
			holder.distance = (TextView) convertView.findViewById(R.id.distandce);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.shop_address = (TextView) convertView.findViewById(R.id.shop_address);
			convertView.setTag(holder);
		} else {
			holder = (ViewHodler) convertView.getTag();
		}

		holder.name.setText(list.get(position).getShopName());
		holder.shop_address.setText(list.get(position).getAddress());
		if (list.get(position).getDistance() != null) {
			String distance1 = list.get(position).getDistance();
			double dis = Double.parseDouble(distance1) / 1000;
			if (dis > 1) {
				DecimalFormat df = new DecimalFormat("#.0");
				String format = df.format(dis);
				holder.distance.setText(format + context.getResources().getString(R.string.kilometer));
			} else if (dis > 0 && dis < 1) {
				holder.distance.setText(distance1 + context.getResources().getString(R.string.meter));
			} else if (dis <= 0) {
				holder.distance.setText(context.getResources().getString(R.string.unknown));
			}

		}

		url = list.get(position).getLogo();
		imageLoader.displayImage(url, holder.image, options);

		return convertView;
	}

	class ViewHodler {
		TextView name;
		TextView shop_address;
		TextView distance;
		ImageView image;
	}

}